package com.kingdee.eas.minghua.material.app;

import org.apache.log4j.Logger;
import javax.ejb.*;
import java.rmi.RemoteException;
import com.kingdee.bos.*;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK;
//import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean;
//import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;

import java.lang.String;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.minghua.finance.ApplicationInvoiceFactory;
import com.kingdee.eas.minghua.finance.ApplicationInvoiceInfo;
import com.kingdee.eas.minghua.jczlwh.jczl.OfferItemsFactory;
import com.kingdee.eas.minghua.jczlwh.jczl.OfferItemsInfo;
import com.kingdee.eas.minghua.jczlwh.mj.SfshEnum;
import com.kingdee.eas.minghua.material.BudgetchangeCollection;
import com.kingdee.eas.minghua.material.BudgetchangeEntryCollection;
import com.kingdee.eas.minghua.material.BudgetchangeEntryInfo;
import com.kingdee.eas.minghua.material.BudgetchangeFactory;
import com.kingdee.eas.minghua.material.ZhongbiaoEntryCollection;
import com.kingdee.eas.minghua.material.ZhongbiaoEntryFactory;
import com.kingdee.eas.minghua.material.ZhongbiaoEntryInfo;
import com.kingdee.eas.minghua.material.ZhongbiaoFactory;
import com.kingdee.eas.minghua.material.ZhongbiaoInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.framework.SystemEnum;
import com.kingdee.eas.framework.CoreBillBaseCollection;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.app.CoreBillBaseControllerBean;
import com.kingdee.eas.minghua.material.BudgetchangeInfo;
import com.kingdee.eas.framework.ObjectBaseCollection;

public class BudgetchangeControllerBean extends AbstractBudgetchangeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.minghua.material.app.BudgetchangeControllerBean");

	protected boolean _Audit(Context ctx, String Audit) throws BOSException {
		BudgetchangeInfo budgrchangeinfo;
		try {
			budgrchangeinfo = BudgetchangeFactory.getLocalInstance(ctx).getBudgetchangeInfo(new ObjectUuidPK(Audit));
			if(budgrchangeinfo.getState().equals(SfshEnum.shenhez)){
				budgrchangeinfo.setState(SfshEnum.yes);
				BudgetchangeFactory.getLocalInstance(ctx).save(budgrchangeinfo);
				ZhongbiaoInfo zhongbiaoinfo = ZhongbiaoFactory.getLocalInstance(ctx).getZhongbiaoInfo(new ObjectUuidPK(budgrchangeinfo.getSourceBillId()));
		    	ZhongbiaoEntryCollection zhongbiaocoll = zhongbiaoinfo.getEntrys();
		    	BudgetchangeEntryCollection budgechangeentry = budgrchangeinfo.getEntrys();
		    	for (int i = 0; i < budgechangeentry.size(); i++) {
		    		boolean Stage = true;
		    		BudgetchangeEntryInfo budgenetyinfo = budgechangeentry.get(i);
		    		for (int j = 0; j < zhongbiaocoll.size(); j++) {
		    			ZhongbiaoEntryInfo zhongentyinfo = zhongbiaocoll.get(j);
		    			if(zhongentyinfo.getOfferItem().getId().toString().equals(budgenetyinfo.getBudgetname().getId().toString())){
		    				Stage=false;
		    				zhongentyinfo.setPriceall(budgenetyinfo.getChangeamount());
		    				ZhongbiaoEntryFactory.getLocalInstance(ctx).save(zhongentyinfo);
		    			}
					}
		    		if(Stage){
		    			ZhongbiaoEntryInfo zbinfo = new ZhongbiaoEntryInfo();
		    			OfferItemsInfo offinfo = OfferItemsFactory.getLocalInstance(ctx).getOfferItemsInfo(new ObjectUuidPK(budgenetyinfo.getBudgetname().getId().toString()));
		    			zbinfo.setParent(zhongbiaoinfo);
		    			zbinfo.setOfferItem(offinfo);
		    			zbinfo.setPriceall(budgenetyinfo.getChangeamount());
		    			ZhongbiaoEntryFactory.getLocalInstance(ctx).save(zbinfo);
		    		}
				}
			}
		} catch (EASBizException e) {
			e.printStackTrace();
		}
		return super._Audit(ctx, Audit);
	}

	protected boolean _UnAudit(Context ctx, String UnAudit) throws BOSException {
		BudgetchangeInfo budgrchangeinfo;
		try {
			budgrchangeinfo = BudgetchangeFactory.getLocalInstance(ctx).getBudgetchangeInfo(new ObjectUuidPK(UnAudit));
			budgrchangeinfo.setState(SfshEnum.save);
			BudgetchangeFactory.getLocalInstance(ctx).save(budgrchangeinfo);
		} catch (EASBizException e) {
			e.printStackTrace();
		}
		return super._UnAudit(ctx, UnAudit);
	}
}